package com.programmercarl.greed;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

/**
 * https://leetcode.cn/problems/queue-reconstruction-by-height/
 * 根据身高重建队列 参考
 *
 * @author yulongtian
 * @create 2025-02-07 14:03
 */
public class Test10 {
    public static void main(String[] args) {
        Test10 test10 = new Test10();
//        int[][] people = {{7, 0}, {4, 4}, {7, 1}, {5, 0}, {6, 1}, {5, 2}};
        int[][] people = {{9, 0}, {7, 0}, {1, 9}, {3, 0}, {2, 7}, {5, 3}, {6, 0}, {3, 4}, {6, 2}, {5, 2}};
        System.out.println(Arrays.deepToString(test10.reconstructQueue(people)));
    }

    public int[][] reconstructQueue(int[][] people) {
        Arrays.sort(people, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                if (o1[0] == o2[0]) {
                    return o1[1] - o2[1];
                }
                return o2[0] - o1[0];
            }
        });
        ArrayList<int[]> queue = new ArrayList<int[]>();
        for (int[] person : people) {
            queue.add(person[1], person);
        }
        for (int i = 0; i < queue.size(); i++) {
            people[i] = queue.get(i);
        }

        return people;
    }
}
